package com.example.springstudy;

import com.example.springstudy.filter.MyFilter;
import com.example.springstudy.interceptor.TestIntercept;
import com.example.springstudy.myEventBus.SecondListner;
import com.google.common.base.Preconditions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;

/**
 * @author jiujiu
 * @SpringBootApplication 用来标识，说明为一个springBoot项目的引导类
 * @SpringBootConfiguration 表示为配置文件
 * @Configuration 定义配置类，等价于配置文件
 * @Component 添加到SpringBoot容器当中，进行扫描表示为一个组件
 * @EnableAutoConfiguration
 * @AutoConfigurationPackage 表示把引导类（SpringStudyApplication）及其自包下的东西添加到容器中
 * @Import({AutoConfigurationImportSelector.class})
 * @ComponentScan 会被springBoot容器纳入管理
 * @EnableAspectJAutoProxy 开启AOP，可以解析aop的配置
 */
@SpringBootApplication

@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class SpringStudyApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringStudyApplication.class, args);
    }

    /**
     * 对拦截器进行注册
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new TestIntercept())
                        .addPathPatterns("/**")//拦截的请求
                        .excludePathPatterns("/index");//不拦截
            }
        };
    }
    @Bean
    public Filter myFilter() {
        return new MyFilter();
    }
    @Bean
    public FilterRegistrationBean uploadFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new DelegatingFilterProxy("myFilter"));
        registration.addUrlPatterns("/*");
        registration.setName("myFilter");
        registration.setOrder(1);
        return registration;
    }
}
